﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using V5_Model;

namespace V5_DAL
{
    public class DALUserToArea
    {
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "K_UserToArea");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_UserToArea");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add( ModelUserToArea model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_UserToArea(");
            strSql.Append("id,tousername,touserareapro,touserareacity,touserareacity2,touserurl,topostnum,touserphone,tousertelephone,adddatetime,userid)");
            strSql.Append(" values (");
            strSql.Append("@id,@tousername,@touserareapro,@touserareacity,@touserareacity2,@touserurl,@topostnum,@touserphone,@tousertelephone,@adddatetime,@userid)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@tousername", SqlDbType.NVarChar,50),
					new SqlParameter("@touserareapro", SqlDbType.NVarChar,50),
					new SqlParameter("@touserareacity", SqlDbType.NVarChar,50),
					new SqlParameter("@touserareacity2", SqlDbType.NVarChar,50),
					new SqlParameter("@touserurl", SqlDbType.NVarChar,300),
					new SqlParameter("@topostnum", SqlDbType.NVarChar,50),
					new SqlParameter("@touserphone", SqlDbType.NVarChar,50),
					new SqlParameter("@tousertelephone", SqlDbType.NVarChar,50),
					new SqlParameter("@adddatetime", SqlDbType.DateTime),
					new SqlParameter("@userid", SqlDbType.Int,4)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.tousername;
            parameters[2].Value = model.touserareapro;
            parameters[3].Value = model.touserareacity;
            parameters[4].Value = model.touserareacity2;
            parameters[5].Value = model.touserurl;
            parameters[6].Value = model.topostnum;
            parameters[7].Value = model.touserphone;
            parameters[8].Value = model.tousertelephone;
            parameters[9].Value = model.adddatetime;
            parameters[10].Value = model.userid;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update( ModelUserToArea model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_UserToArea set ");
            strSql.Append("tousername=@tousername,");
            strSql.Append("touserareapro=@touserareapro,");
            strSql.Append("touserareacity=@touserareacity,");
            strSql.Append("touserareacity2=@touserareacity2,");
            strSql.Append("touserurl=@touserurl,");
            strSql.Append("topostnum=@topostnum,");
            strSql.Append("touserphone=@touserphone,");
            strSql.Append("tousertelephone=@tousertelephone,");
            strSql.Append("adddatetime=@adddatetime,");
            strSql.Append("userid=@userid");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@tousername", SqlDbType.NVarChar,50),
					new SqlParameter("@touserareapro", SqlDbType.NVarChar,50),
					new SqlParameter("@touserareacity", SqlDbType.NVarChar,50),
					new SqlParameter("@touserareacity2", SqlDbType.NVarChar,50),
					new SqlParameter("@touserurl", SqlDbType.NVarChar,300),
					new SqlParameter("@topostnum", SqlDbType.NVarChar,50),
					new SqlParameter("@touserphone", SqlDbType.NVarChar,50),
					new SqlParameter("@tousertelephone", SqlDbType.NVarChar,50),
					new SqlParameter("@adddatetime", SqlDbType.DateTime),
					new SqlParameter("@userid", SqlDbType.Int,4)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.tousername;
            parameters[2].Value = model.touserareapro;
            parameters[3].Value = model.touserareacity;
            parameters[4].Value = model.touserareacity2;
            parameters[5].Value = model.touserurl;
            parameters[6].Value = model.topostnum;
            parameters[7].Value = model.touserphone;
            parameters[8].Value = model.tousertelephone;
            parameters[9].Value = model.adddatetime;
            parameters[10].Value = model.userid;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_UserToArea ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public  ModelUserToArea GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,tousername,touserareapro,touserareacity,touserareacity2,touserurl,topostnum,touserphone,tousertelephone,adddatetime,userid from K_UserToArea ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

             ModelUserToArea model = new  ModelUserToArea();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.tousername = ds.Tables[0].Rows[0]["tousername"].ToString();
                model.touserareapro = ds.Tables[0].Rows[0]["touserareapro"].ToString();
                model.touserareacity = ds.Tables[0].Rows[0]["touserareacity"].ToString();
                model.touserareacity2 = ds.Tables[0].Rows[0]["touserareacity2"].ToString();
                model.touserurl = ds.Tables[0].Rows[0]["touserurl"].ToString();
                model.topostnum = ds.Tables[0].Rows[0]["topostnum"].ToString();
                model.touserphone = ds.Tables[0].Rows[0]["touserphone"].ToString();
                model.tousertelephone = ds.Tables[0].Rows[0]["tousertelephone"].ToString();
                if (ds.Tables[0].Rows[0]["adddatetime"].ToString() != "")
                {
                    model.adddatetime = DateTime.Parse(ds.Tables[0].Rows[0]["adddatetime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["userid"].ToString() != "")
                {
                    model.userid = int.Parse(ds.Tables[0].Rows[0]["userid"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,tousername,touserareapro,touserareacity,touserareacity2,touserurl,topostnum,touserphone,tousertelephone,adddatetime,userid ");
            strSql.Append(" FROM K_UserToArea ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,tousername,touserareapro,touserareacity,touserareacity2,touserurl,topostnum,touserphone,tousertelephone,adddatetime,userid ");
            strSql.Append(" FROM K_UserToArea ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_UserToArea";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }

        #endregion  成员方法
    }
}
